package com.jied.duodian.web;

import com.jied.duodian.entity.DdProduct;
import com.jied.duodian.entity.Dutity.Page;
import com.jied.duodian.service.Impl.DdProductServiceImpl;
import com.jied.duodian.util.CommonResult;
import com.jied.duodian.util.SuId;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author LiJie
 * @Date Created on 2020/10/29
 * WWW https://github.com/j6l
 */
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    DdProductServiceImpl ddProductService;
    @Autowired
    SuId suId;

    /**
     * 更新商品信息
     *
     * @param product
     * @return
     */
    @PostMapping("/updataByIdProduct")
    public CommonResult updataByIdProduct(@RequestBody DdProduct product) {
        Integer integer = ddProductService.updataProduct( product );
        return CommonResult.success( integer );
    }

    /**
     * 根据商品id获取商品
     *
     * @param valid
     * @return
     */
    @GetMapping("/get/{valid}")
    public CommonResult getProduct(
            @PathVariable String valid) {
        DdProduct ddProduct = ddProductService.getProductByid( Integer.parseInt( valid ) );
        log.info( "通过id:{}获取{}", valid, ddProduct );

        return CommonResult.success( ddProduct );
    }

    /**
     * 新增商品
     *
     * @param su
     * @return
     */
    @PostMapping("/addProduct")
    public CommonResult addProduct(
            @RequestBody DdProduct su) {
        su.setProductSn( suId.getNewId() );
        su.setCreateTime( new Date() );

        Integer ddProduct = ddProductService.addProduct( su );
        log.info( "新增商品{} 货物号{}", su.getName(), su.getProductSn() );
        return CommonResult.success( ddProduct );
    }


    /**
     * 首页火热商品类型获取商品列表
     *
     * @param lei
     * @return
     */
    @GetMapping("/all")
    public CommonResult getall(
            @RequestParam("lei") String lei) {
        List<DdProduct> productList = ddProductService.getProductByActivity( lei );
        log.info( "通过{}属性,查找了{}条产品", lei, productList.size() );
        return CommonResult.success( productList );
    }

    /**
     * 获取全部商品
     *
     * @param page
     * @return
     */
    @PostMapping("/productpage")
    public CommonResult getallpage(
            @RequestBody Page page) {
        DdProduct product = new DdProduct();
        Page page1 = ddProductService.getallpageList( product, page, 0 );
        log.info( "请求{}条数据,page为{}", page1.getContent().size(), page );
        return CommonResult.success( page );
    }

    /**
     * 获取未审核商品
     *
     * @param page
     * @return
     */
    @PostMapping("/productshpage")
    public CommonResult getallshpage(
            @RequestBody Page page) {
        DdProduct product = new DdProduct();
        Page page1 = ddProductService.getallpageListBysh( product, page );
        log.info( "请求{}条数据,page为{}", page1.getContent().size(), page );
        return CommonResult.success( page );
    }

    /**
     * 商品name查询
     *
     * @param page
     * @param mc
     * @return
     */
    @PostMapping("/productsspage/mc/{mc}")
    public CommonResult mcgetallpage(
            @RequestBody Page page,
            @PathVariable("mc") String mc) {
        DdProduct product = new DdProduct();
        product.setName( mc );
        Page page1 = ddProductService.getallpageList( product, page, 2 );
        log.info( "请求{}条数据,page为{}", page1.getContent().size(), page );
        return CommonResult.success( page );
    }

    /**
     * 商品货号查询
     *
     * @param page
     * @param hh
     * @return
     */
    @PostMapping("/productsspage/hh/{hh}")
    public CommonResult hhgetallpage(
            @RequestBody Page page,
            @PathVariable("hh") String hh) {
        DdProduct product = new DdProduct();
        product.setProductSn( hh );
        Page page1 = ddProductService.getallpageList( product, page, 1 );
        log.info( "请求{}条数据,page为{}", page1.getContent().size(), page );
        return CommonResult.success( page );
    }

    /**
     * 删除商品
     *
     * @param hh
     * @return
     */
    @PostMapping("/del/{hh}")
    public CommonResult sahnchu(
            @PathVariable("hh") Integer hh) {
        Integer integer = ddProductService.delProductByid( hh );
        log.info( "删除{}条数据}", integer );
        return CommonResult.success( integer );
    }

    /**
     * 修改商品审核
     *
     * @param hh
     * @param i
     * @return
     */
    @PostMapping("/xia/{i}/{hh}")
    public CommonResult xiajia(
            @PathVariable("hh") Integer hh,
            @PathVariable("i") Integer i) {
        DdProduct product = new DdProduct();
        product.setId( hh );
        product.setPublishStatus( i );
        Integer integer = ddProductService.updataProduct( product );
        log.info( "修改{}条数据", integer );
        return CommonResult.success( integer );
    }

    /**
     * 修改商品是否上下架
     *
     * @param hh
     * @param i
     * @return
     */
    @PostMapping("/sh/{i}/{hh}")
    public CommonResult shProduct(
            @PathVariable("hh") Integer hh,
            @PathVariable("i") Integer i) {
        DdProduct product = new DdProduct();
        product.setId( hh );
        product.setVerifyStatus( i );
        Integer integer = ddProductService.updataProduct( product );
        log.info( "修改{}条数据", integer );
        return CommonResult.success( integer );
    }


}
